/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.EventoDao;
import edu.upc.clase.demo.entity.Evento;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author HEspirituM
 */
@Repository
public class EventoDaoImpl extends SimpleJdbcDaoSupport implements EventoDao{
    
     private static Logger log = LoggerFactory.getLogger(EventoDaoImpl.class);

    @Autowired
    public EventoDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }

    @Override
    public Integer insertar(Evento evento) {
        getJdbcTemplate().update(
                "insert into eventocab(CoEvento,NoEvento,FeEvento,CoPersona,FeRegistro,FlEstado) values (?, ?, getdate(), ?, getdate(), ?)",
                evento.getCodigoEvento(), evento.getNombreEvento(),evento.getCodigoPersona(),evento.getFlagEstado());
        return getSimpleJdbcTemplate().queryForInt("call identity()");
    }

    @Override
    public void actualizar(Evento evento) {
        getJdbcTemplate().update(
                "update eventocab set CoEvento = ?, NoEvento = ?, FeEvento = getdate(), CoPersona = ?, FeRegistro = getdate(), FlEstado = ? where CoEvento = ?",
                evento.getCodigoEvento(), evento.getNombreEvento(),evento.getCodigoPersona(),evento.getFlagEstado());
    
    }

    @Override
    public void eliminar(Evento evento) {
        getJdbcTemplate().update(
                "delete from eventocab where CoEvento = ?", evento.getCodigoEvento());
    }

    @Override
    public List<Evento> buscarTodos() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Evento buscar(Integer CoEvento) {
         try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select CoEvento, NoEvento, FeEvento, CoPersona from eventocab where CoEvento=?",
                    new BeanPropertyRowMapper<Evento>(Evento.class),CoEvento);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

}
